package com.yy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.yy.common.constant.PmsConstants;
import com.yy.common.core.controller.BaseController;
import com.yy.common.core.domain.AjaxResult;
import com.yy.common.core.page.TableDataInfo;
import com.yy.domain.*;
import com.yy.service.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 订单管理Controller
 * 
 * @author yy
 * @date 2023-10-31
 */
@RestController
@RequestMapping("/pay/order")
public class PayOrderController extends BaseController
{
    @Autowired
    private IPayOrderService payOrderService;
    @Autowired
    private IAssetHouseService assetHouseService;
    @Autowired
    private IAssetHouseBlockService assetHouseBlockService;
    @Autowired
    private IAssetBuildingService assetBuildingService;
    @Autowired
    private IOwnerUserService ownerUserService;
    @Autowired
    private IPayContractService payContractService;



    /**
     *添加收费项目创建订单
     * contractId=1&ids=1&ids=2&ids=4
     */
    @GetMapping("/addOrderNormal")
    @ApiOperation("添加收费项目创建订单")
    public AjaxResult addOrderNormal(@RequestParam("contractId") Long contractId, Long [] ids){
        return toAjax(this.payOrderService.addOrderNormal(contractId,ids));
    }

    /**
     * 查询订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('pay:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(PayOrder payOrder)
    {
        startPage();
        List<PayOrder> list = payOrderService.selectPayOrderList(payOrder);
        return getDataTable(list);
    }
    @GetMapping("getAllContentByHouseId/{houseId}")
    public AjaxResult getAllContentByHouseId(@PathVariable Long houseId){
        //创建返回对象
        Map<String,Object> data=new HashMap<>();
        //根据商铺ID查询商铺信息
        AssetHouse assetHouse=this.assetHouseService.selectAssetHouseById(houseId);
        if (ObjectUtils.isEmpty(assetHouse)){
            return AjaxResult.error("当前商铺信息不存在");
        }
        data.put("assetHouse",assetHouse);
        //根据楼栋ID查询楼栋信息
        AssetBuilding assetBuilding=this.assetBuildingService.selectAssetBuildingById(assetHouse.getBuildingId());
        if(ObjectUtils.isEmpty(assetBuilding))
        {
            return AjaxResult.error("当前楼栋信息不存在");
        }
        data.put("assetBuilding",assetBuilding);
        //根据商圈ID查询商圈信息
        AssetHouseBlock assetHouseBlock=this.assetHouseBlockService.selectAssetHouseBlockById(assetHouse.getBlockId());
        if (ObjectUtils.isEmpty(assetHouseBlock)){
            return AjaxResult.error("当前商圈不存在");
        }
        data.put("assetHouseBlock",assetHouseBlock);
        //根据owner_id查询业主信息
        OwnerUser ownerUser=this.ownerUserService.selectOwnerUserById(assetHouse.getOwnerId());
        if (ObjectUtil.isNull(ownerUser)){
            return AjaxResult.error("业主不存在");
        }
        data.put("ownerUser",ownerUser);

        //根据商铺ID和合同状态查询合同数据
        PayContract payContract=this.payContractService.queryContractByHouseAndState(houseId, PmsConstants.PAY_CONTRACT_STATE_1);
        if (ObjectUtil.isNull(payContract)){
            return AjaxResult.error("当前商铺没有【正常】的合同信息");
        }
        data.put("payContract",payContract);
        //根据合同编号查询待收费的收费订单
        List<PayOrder> payOrderList=this.payOrderService.queryPayOrderByContractCode(payContract.getCode());
        if (ObjectUtil.isNull(payOrderList)){
            return AjaxResult.error("当前商铺没有已生成的收费项目");
        }
        data.put("payOrderList",payOrderList);
        return AjaxResult.success(data);
    }


}
